﻿using CommandProcessor.Dispatcher;
using Data.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PagedList;
using AutoMapper;
using QuanLySanXuat.Models;
using Domain.Commands;
using Core.Common;
using Core.Extensions;
using Data.Models;


namespace QuanLySanXuat.Controllers
{
    public class StorageDetailController : Controller
    {
        private readonly ICommandBus commandBus;
        private readonly IStorageDetailRepository StorageDetailRepository;
        private readonly IOrderSummaryRepository ordersumaryRepository;
        private readonly IOrderProcessRepository orderProcessRepository;
        private readonly IEmployeeRepository employeeRepository;
        private readonly IStockRepository stockRepository;
        private readonly ICostDetailRepository costDetailRepository;
        public StorageDetailController(ICommandBus commandBus, IStorageDetailRepository StorageDetailRepository, IOrderSummaryRepository ordersumaryRepository, IEmployeeRepository employeeRepository, IOrderProcessRepository orderProcessRepository, ICostDetailRepository costDetailRepository, IStockRepository stockRepository)
        {
            this.commandBus = commandBus;
            this.StorageDetailRepository = StorageDetailRepository;
            this.ordersumaryRepository = ordersumaryRepository;
            this.employeeRepository = employeeRepository;
            this.orderProcessRepository = orderProcessRepository;
            this.costDetailRepository = costDetailRepository;
            this.stockRepository = stockRepository;
        }
        //
        // GET: /StorageDetail/

        public ActionResult Index()
        {
            var StorageDetail = StorageDetailRepository.GetAll();
            return View(StorageDetail.ToPagedList(1, 10));
        }
        public ActionResult Create(int? ID)
        {
            ViewData["OrderSummary"] = ordersumaryRepository.GetMany(x=>x.Status!=1);
            ViewData["Employee"] = employeeRepository.GetAll();
            ViewData["Stock"] = stockRepository.GetAll();
            var StorageDetail = StorageDetailRepository.GetById(ID ?? 0);
            var oderForm = Mapper.Map<StorageDetail, StorageDetailFormModels>(StorageDetail);
            return View(oderForm);
        }
        [HttpGet]
        public JsonResult GetDetail(int? ID)
        {
            
            return Json(1, JsonRequestBehavior.AllowGet);
        }
        public ActionResult Save(StorageDetailFormModels frm)
        {
            var form = new StorageDetailFormModels()
            {
                ID = frm.ID,
                BillNumber=frm.BillNumber,
                DateOut=frm.DateOut,
                Employee1ID=frm.Employee1ID,
                Employee2ID=frm.Employee2ID,
                Employee3ID=frm.Employee3ID,
                OrderSummaryID=frm.OrderSummaryID,
                Quantity=frm.Quantity,
                WarehouseID=frm.WarehouseID
            };
            var command = Mapper.Map<StorageDetailFormModels, CreateOrUpdateStorageDetailCommand>(form);
            IEnumerable<ValidationResult> errors = commandBus.Validate(command);
            ModelState.AddModelErrors(errors);
            var result = commandBus.Submit(command);

            //Cap nhat trang thai bang ket thuc san xuat
            var oderProcess = ordersumaryRepository.GetById(frm.OrderSummaryID ?? 0);
            var oderForm = Mapper.Map<OrderSummary, OrderSummaryFormModels>(oderProcess);
            var oderMand = Mapper.Map<OrderSummaryFormModels, CreateOrUpdateOrderSummaryCommand>(oderForm);
            oderMand.Status = 1;
            commandBus.Submit(oderMand);
            return RedirectToAction("Index");
        }
        [HttpPost]
        public ActionResult RemoveItems(string[] ProID)
        {
            //Chuyen trang thai order ve status=2
            List<CreateOrUpdateOrderCommand> lstOrder = new List<CreateOrUpdateOrderCommand>();
            foreach (var e in ProID)
            {
                var orderID = int.Parse(e);
                var comand = StorageDetailRepository.GetById(orderID);
                var oder = comand.OrderSummary.Order;
                oder.Status = 2;
                var oderForm = Mapper.Map<Order, OrderFormModels>(oder);
                var oderMand = Mapper.Map<OrderFormModels, CreateOrUpdateOrderCommand>(oderForm);
                lstOrder.Add(oderMand);
            }
            commandBus.SubmitRange(lstOrder);
            //Xoa 
            List<DeleteStorageDetailCommand> lst = new List<DeleteStorageDetailCommand>();
            foreach (var e in ProID)
            {
                var orderID = int.Parse(e);
                var command = new DeleteStorageDetailCommand { ID = orderID };
                lst.Add(command);
            }
            commandBus.SubmitRange(lst);
           

            var model = StorageDetailRepository.GetAll();
            return PartialView("_StorageDetailList", model.ToPagedList(1, 10));
        }
        [HttpGet]
        [ValidateInput(false)]
        public ActionResult Page(int PageID)
        {
            var oder = StorageDetailRepository.GetAll().OrderBy(x => x.BillNumber);
            return PartialView("_StorageDetailList", oder.ToPagedList(1, PageID));
        }
        [HttpGet]
        public ActionResult Search(string Name)
        {
            IEnumerable<StorageDetail> model;
            if (String.IsNullOrEmpty(Name))
            {
                model = StorageDetailRepository.GetAll();
            }
            else
            {
                model = StorageDetailRepository.GetMany(x => (x.BillNumber.Contains(Name)) || (x.Employee.Name.Contains(Name)) || (x.OrderSummary.Code.Contains(Name)));

            }
            return PartialView("_StorageDetailList", model.ToPagedList(1, 10));
        }
    }
}
